/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package KASystem;

import java.awt.Component;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;

/**
 *
 * @author kristjan
 */
public class AddParticipant extends javax.swing.JPanel {

    ArrayList<Daytrip> daytrips;
    ArrayList<Hotel> hotels;
    Hotel hotel;
    Conference conference;

    /**
     * Creates new form AddParticipant
     */
    public AddParticipant(Conference c) {
        conference = c;
        initComponents();

        createHotelsDaytrips();
    }

    private void createHotelsDaytrips() {
        daytrips = conference.getDaytrips();
        hotels = conference.getHotels();
        hotel = null;

        JCheckBoxMenuItem jcb;

        for (Daytrip d : daytrips) {
            jcb = new JCheckBoxMenuItem();
            jcb.setText(d.toString());
            daytripMenu.add(jcb);
        }

        daytripMenu.add(jSeparator1);
        daytripMenu.add(closeDaytripMenu);
        daytripMenu.setInvoker(this);
        
        ArrayList<Object> hotelObjects = new ArrayList<>();
        
        for(Hotel h: hotels){
            hotelObjects.add(h);
        }
        
        hotelObjects.add(0, "");
        
        hotelComboBox.setModel(new DefaultComboBoxModel(hotelObjects.toArray()));

        updateHotel();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        daytripMenu = new javax.swing.JPopupMenu();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        closeDaytripMenu = new javax.swing.JMenuItem();
        bigInformation = new javax.swing.JLabel();
        cityTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        countryTextField = new javax.swing.JTextField();
        phoneTextField = new javax.swing.JTextField();
        speakerCheckBox = new java.awt.Checkbox();
        arrivalTextField = new javax.swing.JTextField();
        companionNameTextField = new javax.swing.JTextField();
        companyName2 = new javax.swing.JLabel();
        bigCompany1 = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        companyName1 = new javax.swing.JLabel();
        adressTextField = new javax.swing.JTextField();
        city = new javax.swing.JLabel();
        adress = new javax.swing.JLabel();
        phone = new javax.swing.JLabel();
        country = new javax.swing.JLabel();
        departureTextField = new javax.swing.JTextField();
        company = new javax.swing.JLabel();
        companyTextField = new javax.swing.JTextField();
        informationName = new javax.swing.JLabel();
        companyPhoneTextField = new javax.swing.JTextField();
        arrival = new javax.swing.JLabel();
        departure = new javax.swing.JLabel();
        daytripButton = new javax.swing.JButton();
        bigCompany2 = new javax.swing.JLabel();
        hotelComboBox = new javax.swing.JComboBox();
        hotelCheckBox = new javax.swing.JCheckBox();

        closeDaytripMenu.setText("close");
        closeDaytripMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeDaytripMenuActionPerformed(evt);
            }
        });

        bigInformation.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bigInformation.setText("Information");

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        speakerCheckBox.setLabel("Speaker");
        speakerCheckBox.setName("speakerCheckBox");

        companyName2.setText("Phone:");

        bigCompany1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bigCompany1.setText("Companion");

        nameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTextFieldActionPerformed(evt);
            }
        });

        companyName1.setText("Name:");

        adressTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adressTextFieldActionPerformed(evt);
            }
        });

        city.setText("City:");

        adress.setText("Adress:");

        phone.setText("Phone:");

        country.setText("Country:");

        company.setText("Company:");

        informationName.setText("Name:");
        informationName.setToolTipText("");

        arrival.setText("Arrival:");

        departure.setText("Departure:");

        daytripButton.setText("Daytrips");
        daytripButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                daytripButtonActionPerformed(evt);
            }
        });

        bigCompany2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bigCompany2.setText("Hotel");

        hotelComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hotelComboBoxActionPerformed(evt);
            }
        });

        hotelCheckBox.setEnabled(false);
        hotelCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hotelCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(companyName1)
                        .addGap(18, 18, 18)
                        .addComponent(companionNameTextField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(daytripButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bigInformation)
                            .addComponent(bigCompany1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButton1)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(informationName)
                                            .addComponent(adress)
                                            .addComponent(city)
                                            .addComponent(country)
                                            .addComponent(phone)
                                            .addComponent(arrival))
                                        .addGap(24, 24, 24)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(nameTextField)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(speakerCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(adressTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(cityTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(countryTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(phoneTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(arrivalTextField, javax.swing.GroupLayout.Alignment.TRAILING)))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(departure)
                                            .addComponent(company))
                                        .addGap(14, 14, 14)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(departureTextField)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(companyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(companyName2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(companyPhoneTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE))))))
                            .addComponent(bigCompany2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 5, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(hotelComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(hotelCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bigInformation)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(informationName)
                            .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(speakerCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(adress)
                    .addComponent(adressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(city)
                    .addComponent(cityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(country)
                    .addComponent(countryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phone)
                    .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(arrival)
                    .addComponent(arrivalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(departure)
                    .addComponent(departureTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(company)
                    .addComponent(companyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(companyPhoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(companyName2))
                .addGap(18, 18, 18)
                .addComponent(bigCompany1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(companyName1)
                    .addComponent(companionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(daytripButton))
                .addGap(18, 18, 18)
                .addComponent(bigCompany2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(hotelCheckBox)
                        .addGap(41, 41, 41)
                        .addComponent(jButton1))
                    .addComponent(hotelComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Participant p = new Participant(nameTextField.getText(), conference);
        p.setAddress(adressTextField.getText());
        p.setCity(cityTextField.getText());
        p.setCountry(countryTextField.getText());
        p.setPhone(phoneTextField.getText());
        p.setArrival(arrivalTextField.getText());
        p.setDeparture(departureTextField.getText());
        p.setSpeaker(speakerCheckBox.getState());

        if (!companyTextField.getText().isEmpty() && !companyPhoneTextField.getText().isEmpty()) {
            p.setCompany(new Company(companyTextField.getText(), companyPhoneTextField.getText()));
        }

        if (!companionNameTextField.getText().isEmpty()) {
            Companion companion = new Companion(companionNameTextField.getText());

            Component[] cArr = daytripMenu.getComponents();

            JCheckBoxMenuItem tempBox = null;

            for (int i = 0; i < cArr.length; i++) {
                Component c = cArr[i];

                if ("javax.swing.JCheckBoxMenuItem".equals(c.getClass().getCanonicalName())) {
                    tempBox = (JCheckBoxMenuItem) c;
                    if (tempBox.isSelected()) {
                        companion.addDayTrip(daytrips.get(i));
                    }
                }
            }

            p.setCompanion(companion);
        }
        if (hotel != null) {
            p.setHotelRoom(new HotelRoom(hotel));
        }

        conference.addParticipant(p);

        close();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void updateHotel() {
        Object obj = hotelComboBox.getSelectedItem();
        
        if (obj.getClass().getCanonicalName().equals("KASystem.Hotel")) {
            hotel = (Hotel) hotelComboBox.getSelectedItem();
        } else {
            hotel = null;
        }

        if (hotel != null) {

            if (hotel.getAdditionPrice() != 0) {
                hotelCheckBox.setEnabled(true);
                hotelCheckBox.setText("Room Upgrade: " + hotel.getAdditionPrice());
            } else {
                hotelCheckBox.setEnabled(false);
                hotelCheckBox.setText("");
            }
        }
    }

    private void close() {
        JDialog jd = (JDialog) this.getParent().getParent().getParent();
        jd.dispose();
    }

    private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nameTextFieldActionPerformed

    private void daytripButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_daytripButtonActionPerformed
        daytripMenu.setLocation(daytripButton.getLocationOnScreen().x, daytripButton.getLocationOnScreen().y);
        daytripMenu.setVisible(true);
    }//GEN-LAST:event_daytripButtonActionPerformed

    private void closeDaytripMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeDaytripMenuActionPerformed
        daytripMenu.setVisible(false);
    }//GEN-LAST:event_closeDaytripMenuActionPerformed

    private void adressTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adressTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_adressTextFieldActionPerformed

    private void hotelCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hotelCheckBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_hotelCheckBoxActionPerformed

    private void hotelComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hotelComboBoxActionPerformed
        updateHotel();
    }//GEN-LAST:event_hotelComboBoxActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel adress;
    private javax.swing.JTextField adressTextField;
    private javax.swing.JLabel arrival;
    private javax.swing.JTextField arrivalTextField;
    private javax.swing.JLabel bigCompany1;
    private javax.swing.JLabel bigCompany2;
    private javax.swing.JLabel bigInformation;
    private javax.swing.JLabel city;
    private javax.swing.JTextField cityTextField;
    private javax.swing.JMenuItem closeDaytripMenu;
    private javax.swing.JTextField companionNameTextField;
    private javax.swing.JLabel company;
    private javax.swing.JLabel companyName1;
    private javax.swing.JLabel companyName2;
    private javax.swing.JTextField companyPhoneTextField;
    private javax.swing.JTextField companyTextField;
    private javax.swing.JLabel country;
    private javax.swing.JTextField countryTextField;
    private javax.swing.JButton daytripButton;
    private javax.swing.JPopupMenu daytripMenu;
    private javax.swing.JLabel departure;
    private javax.swing.JTextField departureTextField;
    private javax.swing.JCheckBox hotelCheckBox;
    private javax.swing.JComboBox hotelComboBox;
    private javax.swing.JLabel informationName;
    private javax.swing.JButton jButton1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel phone;
    private javax.swing.JTextField phoneTextField;
    private java.awt.Checkbox speakerCheckBox;
    // End of variables declaration//GEN-END:variables
}
